package com.huashi.dealer.modular.controller.api.user;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.commonEntity.StoreUser;
import com.huashi.dealer.commonEntity.User;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.user.bo.BalanceApplyBo;
import com.huashi.dealer.modular.business.user.param.*;
import com.huashi.dealer.modular.business.user.service.UserService;
import com.huashi.dealer.modular.business.user.vo.StatementAccountVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API用户控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API用户控制器")
@RestController("apiUserController")
@RequestMapping("/api/user")
@Validated
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 账号密码登录
     */
    @PostMapping("/accountLogin")
    @Operation(summary = "账号密码登录")
    public CommonResult<UserInfo> accountLogin(@RequestBody UserRequestParam requestParam) {
        try {
            UserInfo userInfo = userService.accountLogin(requestParam);
            return CommonResult.data(userInfo);
        } catch (Exception e) {
            return CommonResult.error("登录失败：" + e.getMessage());
        }
    }

    /**
     * 用户自动登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户自动登录")
    public CommonResult<UserResponseParam> login(@RequestBody UserRequestParam requestParam) {
        try {
            UserResponseParam responseParam = userService.login(requestParam);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("登录失败：" + e.getMessage());
        }
    }

    /**
     * 当前用户详情
     */
    @GetMapping("/detail")
    @Operation(summary = "当前用户详情")
    public CommonResult<UserInfo> detail() {
        try {
            String token = StpUtil.getTokenValue();
            UserInfo responseParam = userService.getUserDetail(token);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取用户详情失败：" + e.getMessage());
        }
    }

    /**
     * 编辑用户信息
     */
    @PostMapping("/edit")
    @Operation(summary = "编辑用户信息")
    public CommonResult<String> edit(@RequestBody User requestParam) {
        try {
            userService.editUserInfo(requestParam);
            return CommonResult.ok("编辑成功");
        } catch (Exception e) {
            return CommonResult.error("编辑失败：" + e.getMessage());
        }
    }

    /**
     * 获取验证码
     */
    @GetMapping("/code")
    @Operation(summary = "获取验证码")
    public CommonResult<UserResponseParam> getCode() {
        try {
            UserResponseParam responseParam = userService.generateCode();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取验证码失败：" + e.getMessage());
        }
    }

    /**
     * 业务员登录
     */
    @PostMapping("/storeUserLogin")
    @Operation(summary = "业务员登录")
    public CommonResult<StoreUser> storeUserLogin(@RequestBody UserRequestParam requestParam) {
        try {
            StoreUser responseParam = userService.storeUserLogin(requestParam);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("业务员登录失败：" + e.getMessage());
        }
    }

    /**
     * 业务员详情
     */
    @GetMapping("/storeUserDetail")
    @Operation(summary = "业务员详情")
    public CommonResult<StoreUserVo> storeUserDetail() {
        try {
            StoreUserVo responseParam = userService.getStoreUserDetail();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取业务员详情失败：" + e.getMessage());
        }
    }

    /**
     * 客户列表
     */
    @GetMapping("/customList")
    @Operation(summary = "客户列表")
    public CommonResult<CustomInfoResponseParam> customList() {
        Page<User> page = PageFactory.defaultPage();
        try {
            CustomInfoResponseParam responseParam= userService.getCustomList(page);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取客户列表失败：" + e.getMessage());
        }
    }

    /**
     * 余额申请
     */
    @PostMapping("/balanceApply")
    @Operation(summary = "余额申请")
    public CommonResult<String> balanceApply(@RequestBody BalanceApplyBo balanceApplyBo) {
            userService.balanceApply(balanceApplyBo.getMoney(),balanceApplyBo.getImages());
            return CommonResult.ok("申请成功");
    }

    /**
     * 对账单
     */
    @GetMapping("/statementAccount")
    @Operation(summary = "对账单")
    public CommonResult<StatementAccountVo> statementAccount(@RequestParam(required = false) String startDate,
                                                           @RequestParam(required = false) String endDate) {
        try {
            StatementAccountVo vo = userService.getStatementAccount(startDate, endDate);
            return CommonResult.data(vo);
        } catch (Exception e) {
            return CommonResult.error("获取对账单失败：" + e.getMessage());
        }
    }
} 